﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using UtilZ.Dotnet.Ex.Log;

namespace DotnetWinFormApp
{
    public partial class TestAsyncAwait : Form
    {
        public TestAsyncAwait()
        {
            InitializeComponent();
        }

        private void TestAsyncAwait_Load(object sender, EventArgs e)
        {
            var redirectAppenderToUI = (RedirectAppender)Loger.GetAppenderByName(null, "RedirectToUI");
            if (redirectAppenderToUI != null)
            {
                redirectAppenderToUI.RedirectOuput += RedirectLogOutput;
            }
        }

        private void RedirectLogOutput(object sender, RedirectOuputArgs e)
        {
            logControl1.AddLog(e.Item.Content, e.Item.Level);

            //string str;
            //try
            //{
            //    str = string.Format("{0} {1}", DateTime.Now, e.Item.Content);
            //}
            //catch (Exception ex)
            //{
            //    str = ex.Message;
            //}

            //logControl.AddLog(str, e.Item.Level);
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            logControl1.Clear();
        }







        private async void btnTest_Click(object sender, EventArgs e)
        {
            Loger.Info("=========================================================================");
            Loger.Info($"111            ManagedThreadId：{Thread.CurrentThread.ManagedThreadId}");
            await AsyncMethod();
            Loger.Info($"666            ManagedThreadId：{Thread.CurrentThread.ManagedThreadId}");
            Loger.Info("-------------------------------------");
        }

        private async Task AsyncMethod()
        {
            var resultFromTimeConsumingMethod = TimeConsumingMethod();
            string result = await resultFromTimeConsumingMethod + " + AsyncMethod. My Thread ID is :" + Thread.CurrentThread.ManagedThreadId;
            Loger.Warn(result);
            Loger.Info($"555            ManagedThreadId：{Thread.CurrentThread.ManagedThreadId}");
        }
        private Task<string> TimeConsumingMethod()
        {
            var task = Task.Run(() =>
            {
                Loger.Info($"222            ManagedThreadId：{Thread.CurrentThread.ManagedThreadId}");
                Thread.Sleep(1000);
                Loger.Info($"333            ManagedThreadId：{Thread.CurrentThread.ManagedThreadId}");
                return "Hello I am TimeConsumingMethod";
            });
            //Thread.Sleep(2000);
            Loger.Info($"444            ManagedThreadId：{Thread.CurrentThread.ManagedThreadId}");
            return task;
        }



        private void btnTest2_Click(object sender, EventArgs e)
        {
            Loger.Info("=========================================================================");

            Loger.Info($"111            ManagedThreadId：{Thread.CurrentThread.ManagedThreadId}");
            Loger.Info($"444            ManagedThreadId：{Thread.CurrentThread.ManagedThreadId}");
            var task = Task.Factory.StartNew(() =>
            {
                Loger.Info($"222            ManagedThreadId：{Thread.CurrentThread.ManagedThreadId}");
                Thread.Sleep(1000);
                Loger.Info($"333            ManagedThreadId：{Thread.CurrentThread.ManagedThreadId}");
                return "Hello I am TimeConsumingMethod";
            }).ContinueWith((str) =>
            {
                this.Invoke(new Action<string>((s) =>
                {
                    string result = s + " + AsyncMethod. My Thread ID is :" + Thread.CurrentThread.ManagedThreadId;
                    Loger.Warn(result);
                    Loger.Info($"555            ManagedThreadId：{Thread.CurrentThread.ManagedThreadId}");
                }), str.Result);
            });
            Loger.Info($"666            ManagedThreadId：{Thread.CurrentThread.ManagedThreadId}");
            Loger.Info("-------------------------------------");
        }

        private async void btnTest3_Click(object sender, EventArgs e)
        {
            var t = Task.Run(() =>
            {
                Thread.Sleep(5000);
                return "Hello I am TimeConsumingMethod";
            });
            label1.Text = await t;
        }
    }
}
